package org.example.service.impl;

import org.example.mapper.ArtifactMapper;
import org.example.ourEnum.ArtifactEnumStatus;
import org.example.pojo.domain.Artifact;
import org.example.pojo.domain.ArtifactShow;
import org.example.service.ArtifactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArtifactServiceImpl implements ArtifactService {

    @Autowired
    private ArtifactMapper artifactMapper;

    @Override
    public List<Artifact> listArtifacts() {
        return artifactMapper.findAll();
    }

    @Override
    public Artifact getArtifactById(Integer artifactId) {
        return artifactMapper.findById(artifactId);
    }

    @Override
    public void addArtifact(Artifact artifact) {
        artifactMapper.insertArtifact(artifact);
    }

    @Override
    public void updateArtifact(Artifact artifact) {
        artifactMapper.updateArtifact(artifact);
    }

    @Override
    public void deleteArtifact(Integer artifactId) {
        artifactMapper.deleteArtifact(artifactId);
    }

    public List<Artifact> listByPage(Integer size,Integer page){
        return artifactMapper.listByLimit(size * page,size);
    }

    public void addShow(ArtifactShow artifactShow){
        if (artifactMapper.checkShowTimeConflict(artifactShow) != null){
            throw new RuntimeException("时间存在冲突");
        }
        // 申请借出
        artifactMapper.addShow(artifactShow);
    }

    @Override
    public List<Artifact> listAvailableArtifacts() {
        return artifactMapper.findAvailableAll();
    }

    public void approveById(Integer id){
        artifactMapper.approveById(id);
        Integer artifactId = artifactMapper.getArtifactIdByShowId(id);
        artifactMapper.changeArtifactStatusByArtifactId(artifactId, ArtifactEnumStatus.loaned);
    }

    public List<ArtifactShow> listBorrow(){
        return artifactMapper.listBorrow();
    }
}
